package com.zjj.learn.designmodel.work.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxyProxy {

    private MySelf mySelf;

    public JdkProxyProxy(MySelf mySelf) {
        this.mySelf = mySelf;
    }

    public MySelf getObject() {
        //获取代理对象
        MySelf proxy = (MySelf) Proxy.newProxyInstance(
                this.getClass().getClassLoader(),
                mySelf.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        //InvocationHandler 代理对象调用处理程序
                        System.out.println("闺蜜说：亲爱的，有个怂货喜欢你，但是他不敢告诉你，让我来告诉你。");
                        System.out.println("闺蜜说：那个人想说");
                        //闺蜜转达男孩的话
                        Object result = method.invoke(mySelf, args);

                        System.out.println("闺蜜说：你觉得怎么样");
                        return result;
                    }
                });
        return proxy;
    }
}
